---@class Battle.GlobalEventSystem
local GlobalEventSystem = Battle.CreateClass("GlobalEventSystem")
Battle.GlobalEventSystem = GlobalEventSystem
local this = GlobalEventSystem

function GlobalEventSystem.OnCreate()
    this.evenList = {}
end
function GlobalEventSystem.OnDestroy()
    this.evenList = nil
end
function GlobalEventSystem.Register(eventType, func, tbl)
    if not this.evenList then return end
    local evList = this.evenList[eventType]
    if  evList == nil then
        evList = {}
        this.evenList[eventType] = evList;
    end
    for k,v in pairs(evList) do
        if v.f == func and v.tb == tbl then
            return
        end
    end
    table.insert(evList,{f = func,tb = tbl});
end
function GlobalEventSystem.UnRegister(eventType, func, tbl)
    if not this.evenList then return end
    local eventList = this.evenList[eventType];
    if eventList == nil then return end
    for k,v in pairs(eventList) do
        if v.f == func then
            if v.tb == nil or v.tb == tbl then
                table.remove(eventList,k)
                break
            end
        end
    end
end
function GlobalEventSystem.BroadCast(name, args)
    if not this.evenList then return end
    local eventFunList = this.evenList[name];
    if eventFunList == nil then return end
    for k,v in pairs(eventFunList) do
        if v and v.f ~= nil then
            if v.tb then
                v.f(v.tb,args)
            else
                v.f(args)
            end
        end
    end
end